package com.customization.huangwm.utils;

import java.sql.Timestamp;
import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Huangwm
 * @description 后台日期时间操作方法
 */
public class DateTools {

    /**
     * 获取当前年
     *
     * @return yyyy
     */
    public String getCurrentYear() {
        String year = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        try {
            year = sdf.format(new Date());
        } catch (Exception e) {
        }
        return year;
    }

    /**
     * 获取当前月份
     *
     * @return M
     */
    public String getCurrentMonth() {
        String month = "";
        SimpleDateFormat sdf = new SimpleDateFormat("M");
        try {
            month = sdf.format(new Date());
        } catch (Exception e) {
        }
        return month;
    }

    /**
     * 获取当前日期的具体日
     *
     * @return dd
     */
    public String getCurrentDay() {
        String day = "";
        SimpleDateFormat sdf = new SimpleDateFormat("dd");
        try {
            day = sdf.format(new Date());
        } catch (Exception e) {
        }
        return day;
    }

    /**
     * 获取指定日期的具体日
     *
     * @return dd
     */
    public String getCurrentDayByDate(String date) {
        String day = "";
        SimpleDateFormat sdf = new SimpleDateFormat("dd");
        try {
            day = sdf.format(StrToDate(date));
        } catch (Exception e) {
        }
        return day;
    }

    /**
     * 获取当前日期
     * @return yyyy-MM-dd
     */
    public String getCurrentDate() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    /**
     * 获取当前时间
     * @return HH:mm:ss
     */
    public String getCurrentTime() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        return sdf.format(date);
    }

    /**
     * 获取当前时间
     * @return HH:mm
     */
    public String getCurrentTime2() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        return sdf.format(date);
    }

    /**
     * 获取当前日期时间
     * @return yyyy-MM-dd HH:mm:ss
     */
    public String getCurrentTimes() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    /**
     * 字符串转换成日期 
     * @param dateStr 
     * @return yyyy-MM-dd 
     */
    public Date StrToDate(String dateStr) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = format.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 对比日期1是否大于日期2
     * @param start
     * @param end
     * @return
     */
    public boolean compare_date(String start, String end) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date dt1 = format.parse(start);
            Date dt2 = format.parse(end);
            if (dt1.getTime() > dt2.getTime()) {
                return true;
            } else if (dt1.getTime() < dt2.getTime()) {
                return false;
            } else {
                return false;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return false;
    }

    /**
     * 计算两个日期之间相差的天数
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public int daysBetweenDate(Date smdate, Date bdate) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            smdate = sdf.parse(sdf.format(smdate));
            bdate = sdf.parse(sdf.format(bdate));
            Calendar cal = Calendar.getInstance();
            cal.setTime(smdate);
            long time1 = cal.getTimeInMillis();
            cal.setTime(bdate);
            long time2 = cal.getTimeInMillis();
            long between_days = (time2 - time1) / (1000 * 3600 * 24);

            return Integer.parseInt(String.valueOf(between_days));
        } catch (ParseException e) {
            return 0;
        }
    }

    /**
     * *字符串的日期格式的计算
     */
    public int daysBetweenDateString(String smdate, String bdate) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar cal = Calendar.getInstance();
            cal.setTime(sdf.parse(smdate));
            long time1 = cal.getTimeInMillis();
            cal.setTime(sdf.parse(bdate));
            long time2 = cal.getTimeInMillis();
            long between_days = (time2 - time1) / (1000 * 3600 * 24);
            return Integer.parseInt(String.valueOf(between_days));
        } catch (ParseException e) {
            return 0;
        }
    }

    /**
     * *字符串的日期格式的计算-返回相差小時數
     */
    public int daysBetweenDateHours(String currenttime, String date2) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Calendar cal = Calendar.getInstance();
            cal.setTime(sdf.parse(currenttime));
            long time1 = cal.getTimeInMillis();
            cal.setTime(sdf.parse(date2));
            long time2 = cal.getTimeInMillis();
            long between_hours = (time2 - time1) / (1000 * 60);
            return Integer.parseInt(String.valueOf(between_hours));
        } catch (ParseException e) {
            return 0;
        }
    }

    /**
     * 生成UUID
     *
     * @return
     */
    public String getUUID() {
        String uuid = UUID.randomUUID().toString().toLowerCase();
        return uuid;
    }

    /**
     * 获取当前日期时间。 yyyy-MM-dd hh:mm:ss
     * @return
     */
    public String getCurrentDateTime() {
        Date newdate = new Date();
        long datetime = newdate.getTime();
        Timestamp timestamp = new Timestamp(datetime);
        return (timestamp.toString()).substring(0, 19);
    }

    /**
     * 获取当前日期时间。 yyyy-MM-dd hh:mm:ss
     * @return
     */
    public String getCurrentDateTime2() {
        Date newdate = new Date();
        long datetime = newdate.getTime();
        Timestamp timestamp = new Timestamp(datetime);
        return (timestamp.toString()).substring(0, 16);
    }

    /**
     * 获取时间戳   格式如：19990101235959
     * @return
     */
    public String getTimestamp() {
        return getCurrentDateTime().replace("-", "").replace(":", "").replace(" ", "");
    }

    /**
     * 获取当月的每周一
     * @return
     * @throws ParseException
     */
    public  List<String> getWeekFirst() throws ParseException {
        List<String> list = new ArrayList<String>();
        String year = getCurrentYear();
        String month = getCurrentMonth();
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        sdf1.setLenient(false);
        SimpleDateFormat sdf2 = new SimpleDateFormat("EEE");
        for (int i = 1; i < 31 ; i++) {
            Date date = sdf1.parse(year + "-"+month+"-" + i);
            if("星期一".equals(sdf2.format(date))){
                list.add(sdf1.format(date));
            }
        }
        return list;
    }

    /**
     * 获取指定年月份的某一周的第一天
     * @param year
     * @param month
     * @param week
     * @return dateStr
     */
    public String getWeekFirstday(String year,String month,String week){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR,Integer.parseInt(year));//设置年
        c.set(Calendar.MONTH, Integer.parseInt(month)-1);//设置月
        c.set(Calendar.WEEK_OF_MONTH, Integer.parseInt(week));//设置星期
        c.set(Calendar.DAY_OF_WEEK, 2);//本周第一天，以星期一开始
        return(sdf.format(c.getTime()));
    }

    /**
     * 獲取lastday后的日期
     * @param lastday
     * @return
     */
    public String getDateFormCurrentDateToLast(int lastday){
        Format formatter=new SimpleDateFormat("yyyy-MM-dd");
        Date todayDate=new Date();
        //指定lastday后的時間
        long afterTime = (todayDate.getTime()/1000)+60*60*24*lastday;
        todayDate.setTime(afterTime*1000);
        String afterDate=formatter.format(todayDate);
        return afterDate;
    }
    /**
     * 根据指定日期獲取lastday后的日期
     * @param currentdate
     * @param lastday
     * @return
     */
    public String getDateFormCurrentDateToLast(String currentdate,int lastday) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date todayDate = sdf.parse(currentdate);
        Format formatter=new SimpleDateFormat("yyyy-MM-dd");
        //指定lastday后的時間
        long afterTime = (todayDate.getTime()/1000)+60*60*24*lastday;
        todayDate.setTime(afterTime*1000);
        String afterDate=formatter.format(todayDate);
        return afterDate;
    }

    /**
     * 获取当前日期是周几
     * @return
     */
    public String getWeekOfDate() {
        Date date = new Date();
        String[] weekDays = { "6", "0", "1", "2", "3", "4", "5" };
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    /**
     * 当前日期对象所在月份的最后一个星期五
     * @return
     */
    public String getLastWeekDayOfMonth(){
        // 获取当前日期对象
        Date now = new Date();
        // 获取当前日期对象所在月份
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        int month = calendar.get(Calendar.MONTH);
        // 构造当前日期对象所在月份的第一天的日期对象
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        Date firstDayOfMonth = calendar.getTime();
        // 计算当前日期对象所在月份的第一个星期五的日期对象
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        int daysUntilFirstFriday = dayOfWeek >= Calendar.FRIDAY ?
                Calendar.FRIDAY + 7 - dayOfWeek : Calendar.FRIDAY - dayOfWeek;
        calendar.add(Calendar.DAY_OF_MONTH, daysUntilFirstFriday);
        Date firstFriday = calendar.getTime();
        // 计算当前日期对象所在月份的最后一个星期五的日期对象
        calendar.setTime(now);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        int daysUntilLastFriday = dayOfWeek <= Calendar.FRIDAY ?
                Calendar.FRIDAY - dayOfWeek : Calendar.FRIDAY + 7 - dayOfWeek;
        calendar.add(Calendar.DAY_OF_MONTH, daysUntilLastFriday);
        Date lastFriday = calendar.getTime();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(lastFriday);
    }

    /**
     * 当前日期对象所在月份的第一个星期五
     * @return
     */
    public String getFirstWeekDayOfMonth(){
        // 获取当前日期对象
        Date now = new Date();
        // 获取当前日期对象所在月份
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        int month = calendar.get(Calendar.MONTH);
        // 构造当前日期对象所在月份的第一天的日期对象
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        Date firstDayOfMonth = calendar.getTime();
        // 计算当前日期对象所在月份的第一个星期五的日期对象
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        int daysUntilFirstFriday = dayOfWeek >= Calendar.FRIDAY ?
                Calendar.FRIDAY + 7 - dayOfWeek : Calendar.FRIDAY - dayOfWeek;
        calendar.add(Calendar.DAY_OF_MONTH, daysUntilFirstFriday);
        Date firstFriday = calendar.getTime();
        System.out.println("当前日期对象所在月份的第一个星期五：" + firstFriday);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(firstFriday);
    }
}
